<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="el-card__header">
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
            <el-breadcrumb-item>活动列表</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-form :inline="true" label-width="70px">
          <el-form-item label="目录名称">
            <el-input
              :style="{ width: '200px' }"
              v-model="searchs.directoryName"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchs.state" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" @click="clearform">清除</el-button>
            <el-button type="primary" size="mini" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">

            <el-button
              type="text"
              @click="$router.push(`/subjects/list`)"
              v-if="$route.query.id"
            >
              <i class="el-icon-back"></i>
              <span>返回学科</span></el-button
            >
            <el-button type="success" size="small" @click="add">
              <i class="el-icon-edit"></i>
              <span>新增目录</span>
            </el-button>
          </el-form-item>
        </el-form>
        <el-alert :title="counts" type="info" show-icon :closable="false">
        </el-alert>
        <el-table :data="newdata" border style="width: 100%"
          >>
          <el-table-column
            label="序号"
            type="index"
            width="80"
            :index="indexMethod"
          />
          <el-table-column label="所属学科" prop="subjectName" width="160" />
          <el-table-column label="目录名称" prop="directoryName" width="160">
          </el-table-column>
          <el-table-column label="创造者" prop="username" />
          <el-table-column
            label="创建日期"
            prop="addDate"
            width="200"
            :formatter="formatterDate"
          />
          <el-table-column label="面试题数量" prop="totals" />
          <el-table-column
            label="状态"
            prop="state"
            :formatter="formatterstate"
          >
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="">
            <template #default="{ row }">
              <el-button type="text" size="medium " @click="not(row)">{{
                row.state ? "启用" : "禁用"
              }}</el-button>
              <el-button
                type="text"
                size="medium "
                :disabled="!row.state"
                @click.native="revise(row.id)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="medium "
                :disabled="!row.state"
                @click.native="del(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div
          class="pager"
          style="margin: 10px; display: flex; justify-content: flex-end"
        >
          <el-pagination
            :current-page="page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pagesize"
            layout=" sizes, prev, pager, next, jumper"
            :page-count="pages"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            v-if="show"
          />
        </div>
        <directorys
          :dialogFormVisible.sync="dialogFormVisible"
          :title="title"
          :ruleForm.sync="ruleForm"
        ></directorys>
      </el-card>
    </div>
  </div>
</template>

<script>
import directorys from '../components/directorys-add.vue'
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
import {
  list as getDirectorylisting,
  remove,
  detail,
  changeState
} from '@/api/hmmm/directorys'
import { parseTime } from '@/utils/index'

export default {
  components: {
    directorys
  },
  data () {
    return {
      directory: {
        dirvalue: ''
      },
      list: [],
      ruleForm: {
        subjectID: '',
        directoryName: '',
        id: ''
      },
      dialogFormVisible: false,

      page: 1,
      pages: 0,
      pagesize: 10,
      total: 0,
      title: '',
      rulse: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属科目', trigger: 'blur' }
        ]
      },
      simplealist: [],
      counts: '',
      searchs: {
        directoryName: '',
        state: null,
        page: 1,
        pagesize: 10
      },
      searchs1: {
        subjectID: this.$route.query.id,
        state: null,
        page: 1,
        pagesize: 10
      },
      newdata: [],
      show: true
    }
  },
  created () {
    getCompanys()
    getQuestion()
    this.getDirectorylistinga()
    this.simplea()
  },
  methods: {
    async getDirectorylistinga () {
      const { data } = await getDirectorylisting({
        page: this.page,
        pagesize: this.pagesize
      })
      this.pages = data.pages
      this.total = data.counts
      this.list = data.items
      this.counts = `数据一共 ${data.counts} 条`
      this.newdata = data.items
      if (!this.$route.query.id) {
        this.newdata = data.items
      } else {
        this.filters()
      }
    }, // 学科列表
    async simplea () {
      const { data } = await simple()
      this.simplealist = data
    },
    formatterstate (row, column, cellValue, index) {
      if (cellValue === 1) {
        return '已禁用'
      } else if (cellValue === 0) {
        return '已启用'
      }
    },
    // 日期处理
    formatterDate (row, column) {
      return parseTime(row.addDate)
    },
    // 清除输入框和下拉框
    clearform () {
      this.searchs = {
        directoryName: '',
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 序号
    indexMethod (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getDirectorylistinga()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getDirectorylistinga()
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          // await remove({ id })
          await remove({ id: id })
          this.getDirectorylistinga()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    },
    //  打开弹窗
    add () {
      this.dialogFormVisible = true
      this.title = '新增目录'
    },

    // 修改
    async revise (id) {
      this.dialogFormVisible = true
      this.title = '修改目录'
      const { data } = await detail({ id })
      this.ruleForm = data
    },
    async not (row) {
      const state = Number(!row.state)
      await changeState({ id: row.id, state: state })
      this.getDirectorylistinga()
      this.$message({
        message: '恭喜你，更改成功',
        type: 'success'
      })
    },
    // 查找
    async search () {
      const { data } = await getDirectorylisting(this.searchs)
      this.newdata = data.items
      this.searchs.directoryName ? (this.show = false) : (this.show = true) &&
      this.searchs.state ? (this.show = false) : (this.show = true)
    },
    async  filters () {
      const { data } = await getDirectorylisting(this.searchs1)
      this.newdata = data.items
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 10px;
}
::v-deep.el-card__header {
  padding: 0px 15px 20px 20px;
  box-sizing: border-box;
  border-bottom: 1px solid #ebeef5;
}
</style>
